package com.tulele.vtool.handler;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.tulele.vtool.model.BPosterHistory;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.BPosterHistoryService;
import com.tulele.vtool.utils.SysConstant;
import io.micrometer.core.instrument.util.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.tulele.vtool.utils.AesExpand.aesEncrypt;

/**
 * 海报生成记录表(BPosterHistory)表控制层
 *
 * @author makejava
 * @since 2020-12-01 10:28:23
 */
@RestController
@RequestMapping("bPosterHistory")
public class BPosterHistoryHandler {
    /**
     * 服务对象
     */
    @Autowired
    private BPosterHistoryService bPosterHistoryService;

    /**
     * 查询多条数据
     * 127.0.0.1:8070/bPosterHistory/selectAllByLimit
     *
     * @param bPosterHistory 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/selectAllByLimit", produces = "application/json;charset=UTF-8")
    public RespData selectAllByLimit(@RequestHeader("userId") String userId, @RequestHeader("identifying") String springProfilesActive, @RequestBody BPosterHistory bPosterHistory) {
        RespData respData = new RespData();

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(springProfilesActive) || bPosterHistory == null) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Integer pageNum = bPosterHistory.getPageNum();
        if (pageNum == null || pageNum.intValue() <= 0) {
            bPosterHistory.setPageNum(1);
        }

        Integer pageSize = bPosterHistory.getPageSize();
        if (pageSize == null || pageSize.intValue() <= 0) {
            bPosterHistory.setPageSize(10);
        }

        Integer userIdInt = Integer.valueOf(userId);

        bPosterHistory.setUserId(userIdInt);
        bPosterHistory.setGzhCode(springProfilesActive);
        PageInfo<BPosterHistory> pageInfo = this.bPosterHistoryService.selectAllByLimit(bPosterHistory);

        if (null == pageInfo.getList() || pageInfo.getList().size() <= 0) {
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("暂无更多");
            return respData;
        }

        List<BPosterHistory> queryAllByLimitDto = pageInfo.getList();

        if (CollectionUtils.isNotEmpty(queryAllByLimitDto)) {
            List<BPosterHistory> collect = queryAllByLimitDto.stream().map(info -> {
                try {
                    BPosterHistory temporary = new BPosterHistory();

                    // 加密id
                    temporary.setInfoKey(aesEncrypt(info.getPosterId().toString(), SysConstant.KEY));
                    temporary.setResult(info.getResult());

                    return temporary;
                } catch (Exception e) {
                    e.printStackTrace();

                    return null;
                }
            }).collect(Collectors.toList());

            Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(2);
            resultMap.put("msg", pageInfo.isIsLastPage());
            resultMap.put("collect", collect);

            respData.setData(resultMap);
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("查询成功");
            return respData;
        }

        respData.setCode(ReturnCode.CLIENT_ERROR);
        respData.setMsg("暂无更多");
        return respData;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public BPosterHistory selectOne(Integer id) {
        return this.bPosterHistoryService.queryById(id);
    }

}